import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class PassageTimetable extends javax.swing.JFrame implements
		ActionListener
{
	static PrintWriter out;
	static FileReader reader = null;
	
	{
		//Set Look & Feel
		try
		{
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
					.getSystemLookAndFeelClassName());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private JPanel jPanel1;
	private JScrollPane jScrollPane1;
	private JTextArea TimeTable;
	private JButton Getit;
	private JComboBox Time;
	private JComboBox Route;
	private String routeName;
	private String timeName;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				PassageTimetable inst = new PassageTimetable();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public PassageTimetable()
	{
		super();
		initGUI();
	}

	private void initGUI()
	{
		try
		{
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setName("");
			this.setTitle("Timetable");
			{
				jPanel1 = new JPanel();
				getContentPane().add(jPanel1, BorderLayout.CENTER);
				jPanel1.setLayout(null);
				jPanel1.setPreferredSize(new java.awt.Dimension(363, 230));
				jPanel1.setName("Timetable");
				{
					ComboBoxModel RouteModel = new DefaultComboBoxModel(new String[] {
							"Route", "383", "384", "358out", "358back" });
					Route = new JComboBox();
					jPanel1.add(Route);
					Route.setModel(RouteModel);
					Route.setBounds(43, 14, 132, 23);
				}
				{
					ComboBoxModel TimeModel = new DefaultComboBoxModel(new String[] {
							"Weekdays", "Saturday", "Sunday" });
					Time = new JComboBox();
					jPanel1.add(Time);
					Time.setModel(TimeModel);
					Time.setBounds(200, 13, 137, 24);
				}
				{
					Getit = new JButton();
					jPanel1.add(Getit);
					Getit.setText("Get it !");
					Getit.setBounds(366, 12, 87, 25);
					Getit.addActionListener(this);
				}
				{
					jScrollPane1 = new JScrollPane();
					jPanel1.add(jScrollPane1);
					jScrollPane1.setBounds(17, 62, 483, 171);
					{
						TimeTable = new JTextArea();
						jScrollPane1.setViewportView(TimeTable);
						TimeTable.setBounds(17, 62, 327, 156);
						TimeTable.setPreferredSize(new java.awt.Dimension(1000, 280));
					}
				}
			}
			pack();
			this.setSize(522, 275);
		}
		catch (Exception e)
		{
			//add your error handling code here
			e.printStackTrace();
		}
	}

	public void actionPerformed(ActionEvent event)
	{
		routeName = (String) Route.getSelectedItem();
		timeName = (String) Time.getSelectedItem();
		if (routeName.equals("Route") || timeName.equals("Weekdays"))
		{
			Object[] options = { "OK" };
			JOptionPane.showOptionDialog(null,
					"Please select the Route and the Time you want to check", "Warning",
					JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null,
					options, options[0]);
		}
		else
		{
		//System.out.println(routeName + " " + timeName);
    
		int route = TimetableInfo.findRoute(routeName);
    if (timeName == "Weekdays")
    Timetables.showTimetable(route, TimetableInfo.timetableKind.weekday);
     if (timeName == "Saturday")
    Timetables.showTimetable(route, TimetableInfo.timetableKind.saturday);
      if (timeName == "Sunday")
    Timetables.showTimetable(route, TimetableInfo.timetableKind.sunday);
		try
    {
      reader = new FileReader("routetimetables.txt");
      TimeTable.read(reader, "routetimetables.txt");
    }
    catch (IOException exception)
    {
      System.err.println("Load oops");
      exception.printStackTrace();
    }
    finally
    {
      if (reader != null)
      {
        try
        {
          reader.close();
        }
        catch (IOException exception)
        {
          System.err.println("Error closing reader");
          exception.printStackTrace();
        }
      }
    }
	}
	}
}
